package model;

import java.util.*;

public class Machine {
	private long ID;
	private String name;
	private String description;

	/**
	 * Link between Machine and Reparation is 1----0..*
	 */
	private List<Reparation> reparations;

	/**
	 * Link between Machine and MachineType is 0..*----->1
	 */
	private MachineType machineType = null;

	/**
	 * @param ID -
	 *            7 digits number from specific Machines Manufacture.
	 * @param name -
	 *            Manufacture name of machine
	 * @param description -
	 *            some comments of what this machine done for.
	 * @param machineType -
	 *            is this machine made in same company??
	 * 
	 * @author Osahon Idehen
	 */

	public Machine(long ID, String name, String description,
			MachineType machineType) {
		this.ID = ID;
		this.name = name;
		this.description = description;
		this.machineType = machineType;

		reparations = new ArrayList<Reparation>();
		machineType.addMachine(this);
	}

	/**
	 * @return the iD
	 * 
	 * @author Osahon Idehen
	 */
	public long getID() {
		return ID;
	}

	/**
	 * @param id
	 *            the iD to set
	 * 
	 * @author Osahon Idehen
	 */
	public void setID(long id) {
		ID = id;
	}

	/**
	 * @return the name
	 * 
	 * @author Osahon Idehen
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 * 
	 * @author Osahon Idehen
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the description
	 * 
	 * @author Osahon Idehen
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 * 
	 * @author Osahon Idehen
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * adds a reparation to this Machine and set the machine of the reparation
	 * to this machine
	 * 
	 * @author Osahon Idehen
	 */
	public void addReparation(Reparation reparation) {
		if (!reparations.contains(reparation)) {
			reparations.add(reparation);
			reparation.setMachine(this);
		}
	}

	/**
	 * removes a reparation from this Machine and sets the reparation of the
	 * Machine to null
	 * 
	 * @author Osahon Idehen
	 */
	public void removeReparation(Reparation reparation) {
		if (reparations.contains(reparation)) {
			reparations.remove(reparation);
			reparation.setMachine(null);
		}
	}

	/**
	 * @return the reparations
	 * 
	 * @author Osahon Idehen
	 */
	public List<Reparation> getReparations() {
		return reparations;
	}

	/**
	 * @param machineType
	 *            the machineType to set
	 * 
	 * @author Osahon Idehen
	 */
	public void setMachineType(MachineType machineType) {
		this.machineType = machineType;
	}

	/**
	 * @return the machineType
	 * 
	 * @author Osahon Idehen
	 */
	public MachineType getMachineType() {
		return machineType;
	}

	/**
	 * Return the idle time (period of time the machine is out of working) by
	 * getting the reparations duration and adding them up to make a total
	 * 
	 * @return to the total in milliseconds
	 * 
	 * @author Osahon Idehen
	 */

	public long getIdleTimeTotal() {
		long total = 0;
		// gets all the reparation one after the other
		for (Reparation r : getReparations()) {
			// adds the duration to the total
			total += r.getDuration();
		}
		return total;
	}

	/**
	 * Gets the Idle Time within a year and a month for a machine.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public long getIdleTimeTotal(GregorianCalendar date) {
		long total = 0;
		// gets all the reparation one after the other
		for (Reparation r : getReparations()) {
			// adds the duration to the total
			total += r.getIdleTime(date);
		}
		return total;
	}

	/**
	 * Makes a Nice-And-Pretty String from the getIdleTimeTotal().
	 * 
	 * @return to the Nice-And-Pretty String.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public String getIdleTimeTotalDetails(long idleTime) {
		if (!reparations.isEmpty())
			return reparations.get(0).makeNiceDurPrint(idleTime);
		else
			return "";
	}

	/**
	 * Do it later.
	 * 
	 * @return
	 * 
	 * @author Csaba Tarkanyi
	 */
	public String getReparationHistoryPrint() {
		// TODO code here
		return "<Reparations>";
	}

	public String toString() {
		return ID + " " + name + " " + description;
	}
}